package io.zhengsh.juc._1lock.semaphore;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class MainTest {

    public static void main(String[] args) {
        Bridge bridge = new Bridge();
        bridge.setAddress("云南");
        bridge.setName("XX 桥");
        bridge.setMax(5);

        Semaphore semaphore = new Semaphore(bridge.getMax());
        for (int i=0; i< 100; i++) {
            int idx = i;
            new Thread(()-> {
                try {
                    Migrator migrator = new Migrator();
                    migrator.setName("name-" + idx);
                    semaphore.acquire();
                    TimeUnit.SECONDS.sleep(1);
                    migrator.gapBridge();
                    System.out.println("name " + migrator.getName() + " 通过");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    semaphore.release();
                }
            }).start();
        }
    }
}
